package com.zm.eureka.cloudhytrix.controller;


import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.zm.bean.User;
import com.zm.feign.EurekaClientInterface;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
@RestController
@RequestMapping("/test")
public class HystrixController {
    @Resource
    private EurekaClientInterface eurekaClientInterface;
    @HystrixCommand(fallbackMethod = "findByIdFallback")
    @GetMapping("/user/{id}")
    public User findById(@PathVariable Long id) {
        return eurekaClientInterface.findById(id);
    }

    public User findByIdFallback(Integer id){
        User user=new User();
        user.setId(-1L);
        user.setName("默认用户");
        return user;
    }

    //////////////////////////////////////////////////////////
    @HystrixCommand(fallbackMethod = "findByIdFallback")
    @GetMapping("/user/{id}")
    public String saveUser() {
        User user=new User("zzz",2L);
        return eurekaClientInterface.saveUser(user);
    }

    public User saveUserFallback(){
        User user=new User();
        user.setId(-1L);
        user.setName("默认用户");
        return user;
    }

}
